home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / sys / dos / bcc / dosbgi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  8.8 KB  |  429 lines

  1. /* $Id: dosbgi.c,v 1.3 1994/08/10 01:16:36 mjl Exp $
  2.    $Log: dosbgi.c,v $
  3.  * Revision 1.3  1994/08/10  01:16:36  mjl
  4.  * Updates to the DOS BGI (BCC) driver, submitted by Paul Casteels.
  5.  *
  6.  * Revision 1.2  1994/05/25  22:08:19  mjl
  7.  * Win3 driver added, other changes to bring DOS/BGI (bcc) support
  8.  * up to date, contributed by Paul Casteels.
  9.  *
  10.  * Revision 1.8  1993/03/15  21:39:04  mjl
  11.  * Changed all _clear/_page driver functions to the names _eop/_bop, to be
  12.  * more representative of what's actually going on.
  13.  *
  14.  * Revision 1.7  1993/03/03  19:41:54  mjl
  15.  * Changed PLSHORT -> short everywhere; now all device coordinates are expected
  16.  * to fit into a 16 bit address space (reasonable, and good for performance).
  17.  *
  18.  * Revision 1.6  1993/02/27  04:46:31  mjl
  19.  * Fixed errors in ordering of header file inclusion.  "plplot.h" should
  20.  * always be included first.
  21.  *
  22.  * Revision 1.5  1993/02/22  23:10:50  mjl
  23.  * Eliminated the gradv() driver calls, as these were made obsolete by
  24.  * recent changes to plmeta and plrender.  Also eliminated page clear commands
  25.  * from grtidy() -- plend now calls grclr() and grtidy() explicitly.
  26.  *
  27.  * Revision 1.4  1993/01/23  05:41:39  mjl
  28.  * Changes to support new color model, polylines, and event handler support
  29.  * (interactive devices only).
  30.  *
  31.  * Revision 1.3  1992/11/07  07:48:37  mjl
  32.  * Fixed orientation operation in several files and standardized certain startup
  33.  * operations. Fixed bugs in various drivers.
  34.  *
  35.  * Revision 1.2  1992/09/29  04:44:39  furnish
  36.  * Massive clean up effort to remove support for garbage compilers (K&R).
  37.  *
  38.  * Revision 1.1  1992/05/20  21:32:32  furnish
  39.  * Initial checkin of the whole PLPLOT project.
  40.  *
  41. */
  42.  
  43. /*
  44.   dosbgi.c
  45.  
  46.     Adapted from dosvga.c
  47.   Paul Casteels
  48.   25-Sep-1993
  49.  
  50.   A driver for Borland's BGI drivers.
  51.   Compile this with Borland C 3.1
  52.  
  53. */
  54. #ifdef BGI
  55.  
  56. #include "plplotP.h"
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include "drivers.h"
  60. #include <graphics.h>
  61. #include <process.h>
  62. #include <conio.h>
  63.  
  64. static void pause(PLStream *pls);
  65.  
  66. /* A flag to tell us whether we are in text or graphics mode */
  67.  
  68. #define TEXT_MODE 0
  69. #define GRAPHICS_MODE 1
  70.  
  71. static int mode = TEXT_MODE;
  72.  
  73. #define CLEAN 0
  74. #define DIRTY 1
  75.  
  76. static page_state;
  77.  
  78. static PLDev device;
  79. static PLDev *dev = &device;
  80.  
  81. int gdriver,gmode,errorcode;
  82.  
  83.  
  84. #ifdef TSENG4
  85. #define Mode640x400     0
  86. #define Mode640x480     1
  87. #define Mode800x600     2
  88. #define Mode1024x768    3
  89.  
  90. int Tseng4Mode(void)
  91. {
  92. //  return(Mode640x400);
  93. //  return(Mode640x480);
  94.     return(Mode800x600);
  95. //  return(Mode1024x768);
  96. }
  97. #endif
  98.  
  99. /*----------------------------------------------------------------------*\
  100. * bgi_graph()
  101. *
  102. * Switch to graphics mode.
  103. \*----------------------------------------------------------------------*/
  104.  
  105. void
  106. bgi_graph(PLStream *pls)
  107. {
  108.     char *bgidir;
  109.  
  110.      if (pls->graphx == TEXT_MODE) {
  111.     gdriver = DETECT;
  112.     if ((bgidir = getenv("BGIDIR")) != NULL)
  113.         initgraph(&gdriver,&gmode,bgidir);
  114.     else
  115.         initgraph(&gdriver,&gmode,"\\bc4\\bgi");
  116.     errorcode = graphresult();
  117.     if (errorcode != grOk) {
  118.          printf("Unable to set graphics mode.");
  119.          exit(0);
  120.     }
  121.     pls->graphx = GRAPHICS_MODE;
  122.     page_state = CLEAN;
  123.      }
  124. }
  125.  
  126. /*----------------------------------------------------------------------*\
  127. * bgi_init()
  128. *
  129. * Initialize device.
  130. \*----------------------------------------------------------------------*/
  131.  
  132. void
  133. plD_init_vga(PLStream *pls)
  134. {
  135.     int driver;
  136.  
  137.      pls->termin = 1;            /* is an interactive terminal */
  138.      pls->icol0 = 1;
  139.      pls->width = 1;
  140.      pls->bytecnt = 0;
  141.      pls->page = 0;
  142.      pls->graphx = TEXT_MODE;
  143.      pls->plbuf_write = 1;
  144.  
  145.      if (!pls->colorset)
  146.     pls->color = 1;
  147.  
  148. /* Set up device parameters */
  149.  
  150. #ifdef TSENG4
  151.   driver =installuserdriver ("TSENG4", Tseng4Mode);
  152.   errorcode = graphresult();
  153.  
  154.   errorcode = graphresult();
  155.   if (errorcode != grOk) {
  156.      printf("Graphics error: %s\n", grapherrormsg(errorcode));
  157.      printf("Press any key to halt:");
  158.      getch();
  159.      exit(1); /* terminate with an error code */
  160.   }
  161. #endif
  162.  
  163.      bgi_graph(pls);
  164.  
  165.      dev->xold = UNDEFINED;
  166.      dev->yold = UNDEFINED;
  167.      dev->xmin = 0;
  168.      dev->xmax = getmaxx();
  169.      dev->ymin = 0;
  170.      dev->ymax = getmaxy();
  171.  
  172.      plP_setpxl(2.5, 2.5);           /* Pixels/mm. */
  173.  
  174.      plP_setphy((PLINT) 0, (PLINT) dev->xmax, (PLINT) 0, (PLINT) dev->ymax);
  175.  
  176. }
  177.  
  178. /*----------------------------------------------------------------------*\
  179. * bgi_line()
  180. *
  181. * Draw a line in the current color from (x1,y1) to (x2,y2).
  182. \*----------------------------------------------------------------------*/
  183.  
  184. void
  185. plD_line_vga(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
  186. {
  187.      int x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
  188. /*
  189.      if (pls->pscale)
  190.     plSclPhy(pls, dev, &x1, &y1, &x2, &y2);
  191. */
  192.      y1 = dev->ymax - y1;
  193.      y2 = dev->ymax - y2;
  194.  
  195.      moveto(x1, y1);
  196.      lineto(x2, y2);
  197.  
  198.      page_state = DIRTY;
  199. }
  200.  
  201. /*----------------------------------------------------------------------*\
  202. * bgi_polyline()
  203. *
  204. * Draw a polyline in the current color.
  205. \*----------------------------------------------------------------------*/
  206.  
  207. void
  208. plD_polyline_vga(PLStream *pls, short *xa, short *ya, PLINT npts)
  209. {
  210.      PLINT i;
  211.  
  212.      for (i = 0; i < npts - 1; i++)
  213.     plD_line_vga(pls, xa[i], ya[i], xa[i + 1], ya[i + 1]);
  214. }
  215.  
  216. /*----------------------------------------------------------------------*\
  217. * bgi_eop()
  218. *
  219. * End of page.
  220. \*----------------------------------------------------------------------*/
  221.  
  222. void
  223. plD_eop_vga(PLStream *pls)
  224. {
  225.      if (page_state == DIRTY)
  226.     pause(pls);
  227.      if (pls->graphx == GRAPHICS_MODE) {
  228.         clearviewport();
  229.      }
  230. //    _eopscreen(_GCLEARSCREEN);
  231.      page_state = CLEAN;
  232. }
  233.  
  234. /*----------------------------------------------------------------------*\
  235. * bgi_bop()
  236. *
  237. * Set up for the next page.
  238. * Advance to next family file if necessary (file output).
  239. \*----------------------------------------------------------------------*/
  240.  
  241. void
  242. plD_bop_vga(PLStream *pls)
  243. {
  244.      pls->page++;
  245.      plD_eop_vga(pls);
  246. }
  247.  
  248. /*----------------------------------------------------------------------*\
  249. * bgi_tidy()
  250. *
  251. * Close graphics file or otherwise clean up.
  252. \*----------------------------------------------------------------------*/
  253.  
  254. void
  255. plD_tidy_vga(PLStream *pls)
  256. {
  257. /*
  258.      bgi_text(pls);
  259. */
  260.      closegraph();
  261.      pls->page = 0;
  262.      pls->OutFile = NULL;
  263. }
  264.  
  265. /*----------------------------------------------------------------------*\
  266. * bgi_color()
  267. *
  268. * Set pen color.
  269. \*----------------------------------------------------------------------*/
  270.  
  271. /*
  272. enum COLORS {
  273.         BLACK,
  274.     YELLOW,
  275.         MAGENTA,
  276.     CYAN,
  277.     RED,
  278.     GREEN,
  279.     BLUE,
  280.         LIGHTGRAY,
  281.     DARKGRAY,
  282.     LIGHTBLUE,
  283.     LIGHTGREEN,
  284.         LIGHTCYAN,
  285.         LIGHTRED,
  286.     LIGHTMAGENTA,
  287.     BROWN,
  288.     WHITE
  289. };
  290. //#endif
  291. */
  292.  
  293. void
  294. bgi_color(PLStream *pls)
  295. {
  296.   int i;
  297.      static int cmap[16] = {
  298.     WHITE, RED, YELLOW, GREEN,
  299.     CYAN, WHITE, WHITE, LIGHTGRAY,
  300.     WHITE, BLUE, GREEN, CYAN,
  301.     RED, MAGENTA, YELLOW, WHITE
  302.      };
  303.  
  304.      if (pls->icol0 < 0 || pls->icol0 > 15)
  305.     pls->icol0 = 15;
  306.  
  307. /*
  308.   for (i=0;i<=15;i++)
  309.      setpalette(i,cmap[i]);
  310. */
  311. //  setpalette(0,WHITE);
  312.   setcolor(pls->icol0);
  313.  
  314. //    _remappalette((short) pls->icol0, cmap[pls->icol0]);
  315. //    _setcolor((short) pls->icol0);
  316.  
  317. }
  318.  
  319. void plD_state_vga(PLStream *pls,PLINT op) {
  320. //  PSDev *dev = (PSDev *) pls->dev;
  321.     static int cmap[16] = {
  322.     WHITE, RED, YELLOW, GREEN,
  323.     CYAN, WHITE, WHITE, LIGHTGRAY,
  324.     WHITE, BLUE, GREEN, CYAN,
  325.     RED, MAGENTA, YELLOW, WHITE
  326.      };
  327.  
  328.  
  329.     switch(op) {
  330.     case PLSTATE_WIDTH:
  331.      break;
  332.     case PLSTATE_COLOR0:
  333.      if (pls->color) {
  334.         setcolor(pls->icol0);
  335.      }
  336.      break;
  337.     }
  338. }
  339.  
  340.  
  341. /*----------------------------------------------------------------------*\
  342. * bgi_text()
  343. *
  344. * Switch to text mode.
  345. \*----------------------------------------------------------------------*/
  346.  
  347. void
  348. bgi_text(PLStream *pls)
  349. {
  350.      if (pls->graphx == GRAPHICS_MODE) {
  351.     if (page_state == DIRTY)
  352.          pause(pls);
  353.     restorecrtmode();
  354. //      _setvideomode(_DEFAULTMODE);
  355.     pls->graphx = TEXT_MODE;
  356.      }
  357. }
  358.  
  359. void bgi_text2graph(PLStream *pls)
  360. {
  361.     if (pls->graphx == TEXT_MODE)
  362.     {
  363.       setgraphmode(getgraphmode());    
  364.         pls->graphx = GRAPHICS_MODE;
  365.         page_state == CLEAN;
  366.     }
  367. }
  368.  
  369.  
  370. /*----------------------------------------------------------------------*\
  371. * bgi_width()
  372. *
  373. * Set pen width.
  374. \*----------------------------------------------------------------------*/
  375.  
  376. void
  377. bgi_width(PLStream *pls)
  378. {
  379. }
  380.  
  381. /*----------------------------------------------------------------------*\
  382. * bgi_esc()
  383. *
  384. * Escape function.
  385. \*----------------------------------------------------------------------*/
  386.  
  387. void
  388. plD_esc_vga(PLStream *pls, PLINT op, void *ptr)
  389. {
  390.     switch (op) {
  391.     case PLESC_TEXT:
  392.         bgi_text(pls);
  393.         break;
  394.  
  395.     case PLESC_GRAPH:
  396.         bgi_text2graph(pls);
  397.         break;
  398.     }
  399. }
  400.  
  401. /*----------------------------------------------------------------------*\
  402. * pause()
  403. *
  404. * Wait for a keystroke.
  405. \*----------------------------------------------------------------------*/
  406.  
  407. static void
  408. pause(PLStream *pls)
  409. {
  410.     char *pstr = "pause->";
  411.  
  412.   if (pls->graphx == TEXT_MODE) {
  413.     gotoxy(0,0);
  414.     cprintf(pstr);
  415.   } else
  416.     outtextxy(0,0,pstr);   // are we in graphics mode ??
  417.     while (!kbhit());
  418.     getch();
  419. }
  420.  
  421. #else
  422. int
  423. pldummy_dosbgi()
  424. {
  425.     return 0;
  426. }
  427.  
  428. #endif
  429.